<?php
	require_once("Service.php");
	require_once("Database.php");
	require_once("vo/ForumPostVO.php");
	require_once("vo/ForumCommentVO.php");
	
	/**
	 * Class which handles all forum related queries.
	 */
	class ForumService extends Service {
		
		var $conn;
		
		/** The constructor of this object. */
		function __construct() {
			
			parent::__construct();
			$this->conn = new Database();
		}
		
		/**
		 * Finds a list of forum posts for a group.
		 * @param $sessionID The session id of the user calling this function.
		 * @param $groupUID The group to find posts for.
		 * @return An array of forum posts.
		 */
		function getPostList($sessionID, $groupUID) {
			
			$this->validateSession($sessionID);
			
			// get a list of all the posts for the group
			$query = Database::buildQuery("
				SELECT
					ForumPosts.forumPostUID,
					ForumPosts.heading,
					ForumPosts.dateCreated,
					Users.userUID,
					Users.firstName,
					Users.lastName,
					COUNT(ForumComments.forumCommentUID) AS noComments
				FROM ForumPosts
				INNER JOIN Users
					ON Users.userUID = ForumPosts.userUID
				LEFT OUTER JOIN ForumComments
					ON ForumComments.forumPostUID = ForumPosts.forumPostUID
				WHERE ForumPosts.groupUID = ?
				GROUP BY 
					ForumPosts.forumPostUID,
					ForumPosts.heading,
					ForumPosts.dateCreated,
					Users.userUID,
					Users.firstName,
					Users.lastName
				ORDER BY ForumPosts.dateCreated DESC
			", $groupUID);
			$qryForumPosts = $this->conn->execute($query);
			
			$posts = array();
			while($recForumPosts = mysql_fetch_assoc($qryForumPosts)) {
				$posts[] = $recForumPosts;
			}
			return $posts;
		}
		
		/**
		 * Returns the full details of a forum post.
		 * @param $sessionID The session id of the user calling this function.
		 * @param $forumPostUID The id of the post to retrieve.
		 * @return A object containing the post details.
		 */
		function getPostDetails($sessionID, $forumPostUID) {
			
			$this->validateSession($sessionID);
			
			// get the post details
			$query = Database::buildQuery("
				SELECT
					ForumPosts.forumPostUID,
					ForumPosts.groupUID,
					ForumPosts.heading,
					ForumPosts.message,
					ForumPosts.dateCreated,
					Users.userUID,
					Users.firstName,
					Users.lastName
				FROM ForumPosts
				INNER JOIN Users
					ON Users.userUID = ForumPosts.userUID
				WHERE ForumPosts.forumPostUID = ?
			", $forumPostUID);
			$qryPostDetails = $this->conn->execute($query);
			
			$recPostDetails = mysql_fetch_assoc($qryPostDetails);
			$post = ForumPostVO::mapObject($recPostDetails);
			
			// get the post comments
			$query = Database::buildQuery("
				SELECT
					ForumComments.forumCommentUID,
					ForumComments.forumPostUID,
					ForumComments.message,
					ForumComments.dateCreated,
					Users.userUID,
					Users.firstName,
					Users.lastName
				FROM ForumComments
				INNER JOIN Users
					ON Users.userUID = ForumComments.userUID
				WHERE ForumComments.forumPostUID = ?
				ORDER BY ForumComments.dateCreated ASC
			", $forumPostUID);
			$qryForumComments = $this->conn->execute($query);
			
			$comments = array();
			while($recForumComments = mysql_fetch_assoc($qryForumComments)) {
				$comments[] = ForumCommentVO::mapObject($recForumComments);
			}
			$post->comments = $comments;
			
			return $post;
		}
		
		/**
		 * Saves a new forum post on a group.
		 * @param $sessionID The sessionID of the user calling this function.
		 * @param $groupUID The group on which the forum post was made.
		 * @param $userUID The user who made the forum post.
		 * @param $heading The heading of the post.
		 * @param $message The message body of the post.
		 * @return The forumPostUID of the saved forum post.
		 */
		function savePost($sessionID, $groupUID, $userUID, $heading, $message) {
			
			$this->validateSession($sessionID);
			
			$forumPostUID = $this->conn->generateUID();
			
			// save the forum post
			$query = Database::buildQuery("
				INSERT INTO ForumPosts (
					forumPostUID,
					groupUID,
					userUID,
					heading,
					message,
					dateCreated
				) VALUES (?, ?, ?, ?, ?, ?)
			", $forumPostUID, $groupUID, $userUID, $heading, $message, date("Y-m-d H:i:s"));
			$this->conn->execute($query);
			
			return $forumPostUID;
		}
		
		/**
		 * Saves a comment on a forum post.
		 * @param $sessionID The sessionID of the user calling this function.
		 * @param $forumPostUID The forum post the comment was made on.
		 * @param $userUID The user who made the comment.
		 * @param $message The message body of the comment.
		 * @return The forumCommentUID of the saved forum comment.
		 */
		function saveComment($sessionID, $forumPostUID, $userUID, $message) {
			
			$this->validateSession($sessionID);
			
			$forumCommentUID = $this->conn->generateUID();
			
			// save the forum post comment
			$query = Database::buildQuery("
				INSERT INTO ForumComments(
					forumCommentUID,
					forumPostUID,
					userUID,
					message,
					dateCreated
				) VALUES (?, ?, ?, ?, ?)
			", $forumCommentUID, $forumPostUID, $userUID, $message, date("Y-m-d H:i:s"));
			$this->conn->execute($query);
			
			return $forumCommentUID;
		}
		
		/**
		 * Deletes a forum post from the post list.
		 * @param $sessionID The sessionID of the user calling this function.
		 * @param $forumPostUID The forum post to censor.
		 */
		function censorPost($sessionID, $forumPostUID) {
			
			$this->validateSession($sessionID);
			
			$queries = array();
			
			// delete comments made on the post
			$queries[] = Database::buildQuery("
				DELETE FROM ForumComments
				WHERE forumPostUID = ?
			", $forumPostUID);
			
			// delete the post
			$queries[] = Database::buildQuery("
				DELETE FROM ForumPosts
				WHERE forumPostUID = ?
			", $forumPostUID);
			
			$this->conn->transaction($queries);
		}
		
		/**
		 * Deletes a forum comment from a post.
		 * @param $sessionID The sessionID of the user calling this function.
		 * @param $forumCommentUID The forum post to censor.
		 */
		function censorComment($sessionID, $forumCommentUID) {
			
			$this->validateSession($sessionID);
			
			// delete the comment
			$query = Database::buildQuery("
				DELETE FROM ForumComments
				WHERE forumCommentUID = ?
			", $forumCommentUID);
			$this->conn->execute($query);
		}
	}
?>